package cn.cmdr.controller;

import cn.cmdr.pojo.*;
import cn.cmdr.service.ComService;
import cn.cmdr.service.JournalService;
import cn.cmdr.service.SortService;
import cn.cmdr.vo.ComVo;
import cn.cmdr.vo.ResultVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/sys/cla")
public class ClaController {
    @Resource
    private SortService sortService;
    @Resource
    private ComService comService;
    @Resource
    private JournalService journalService;
    @PostMapping("/list/{pageSize}/{pageNo}")
    public ResultVo getList(@PathVariable Integer pageSize, @PathVariable Integer pageNo, @RequestBody Sort sort){
        return ResultVo.sucess(sortService.getSortPageList(pageSize,pageNo,sort));
    }
    @PostMapping("/update")
    @RequiresPermissions("cla:update")
    public ResultVo update(@RequestBody Sort sort){
        try {
            sortService.updateSort(sort);
            Journal journal = new Journal();
            journal.setStaffId(((Staff) SecurityUtils.getSubject().getPrincipal()).getId());
            journal.setUpDate(new Date());
            journal.setUpContent("修改分类:"+sort.getSortName());
            journal.setMenuId(2);
            journalService.add(journal);
            return ResultVo.sucess("修改成功");
        }catch (Exception e){
            return ResultVo.fail("修改失败",e);
        }
    }
    @PostMapping("/delete")
    @RequiresPermissions("cla:del")
    public ResultVo delete(String id){
        try {
            List<Commodity> list = comService.getsById(Integer.parseInt(id));
            Sort sort = sortService.getSort(Integer.parseInt(id));
            if(list.size()!=0){
                return ResultVo.fail(sort.getSortName()+"下还有商品不能删除");
            }else{
                sortService.deleteSort(Integer.parseInt(id));
                Journal journal = new Journal();
                journal.setStaffId(((Staff) SecurityUtils.getSubject().getPrincipal()).getId());
                journal.setUpDate(new Date());
                journal.setUpContent("删除分类:"+sort.getSortName());
                journal.setMenuId(2);
                journalService.add(journal);
            }
            return ResultVo.sucess("删除成功");
        }catch (Exception e){
            return ResultVo.fail("删除失败",e);  //包含了打印错误日志
        }
    }
    @PostMapping("/add")
    @RequiresPermissions("cla:add")
    public ResultVo add(@RequestBody Sort sort){
        try {
            sortService.add(sort);
            Journal journal = new Journal();
            journal.setStaffId(((Staff) SecurityUtils.getSubject().getPrincipal()).getId());
            journal.setUpDate(new Date());
            journal.setUpContent("添加分类:"+sort.getSortName());
            journal.setMenuId(2);
            journalService.add(journal);
            return ResultVo.sucess("添加类别成功");
        }catch (Exception e){
            return ResultVo.fail("添加类别失败",e);  //包含了打印错误日志
        }
    }


    @PostMapping("/sortlist")
    public ResultVo sortList(Integer disp){
        return ResultVo.sucess(sortService.getSortList(disp));
    }

}
